package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Autowired
    private AdSensitiveMapper adSensitiveMapper;

    @Override
    public ResponseResult findPage(SensitiveDto dto) {

        //参数检测
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

        //根据名称模糊查询
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }

        //打包
        IPage result= page(page,lambdaQueryWrapper);

        //返回结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult addSensitive(AdSensitive adSensitive) {
        //参数校验
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //添加当前时间为创建时间
        adSensitive.setCreatedTime(new Date());
        boolean b = save(adSensitive);

        //判断是否操作成功
        if (b){
            //成功返回
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            //失败返回
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult updateAdSensitive(AdSensitive adSensitive) {
        //参数校验
        if (adSensitive == null || adSensitive.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //修改
        boolean b = updateById(adSensitive);

        //判断是否操作成功
        if (b){
            //成功返回
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

    }

    @Override
    public ResponseResult delById(Integer id) {

        //校验参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //根据id查询
        AdSensitive adSensitive = this.getById(id);

        //判断数据是否存在
        if (adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //删除敏感词
        removeById(id);

        //返回状态码
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public List<AdSensitive> findAllSensitive() {
        return adSensitiveMapper.findAll();
    }

}
